﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;
using System.Windows.Controls;

namespace Kokomo.Controls
{
	public class DialogPresenter : FrameworkElement
	{
		public object DialogContent
		{
			get { return (object)GetValue(DialogContentProperty); }
			set { SetValue(DialogContentProperty, value); }
		}

		// Using a DependencyProperty as the backing store for DialogContent.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty DialogContentProperty =
			DependencyProperty.Register("DialogContent", typeof(object), typeof(DialogPresenter), new UIPropertyMetadata(null));



		public DataTemplate DialogContentTemplate
		{
			get { return (DataTemplate)GetValue(DialogContentTemplateProperty); }
			set { SetValue(DialogContentTemplateProperty, value); }
		}

		// Using a DependencyProperty as the backing store for DialogContentTemplate.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty DialogContentTemplateProperty =
			DependencyProperty.Register("DialogContentTemplate", typeof(DataTemplate), typeof(DialogPresenter), new UIPropertyMetadata(null));

		public Window HostWindow { get; private set; }

		public bool IsOpen
		{
			get { return (bool)GetValue(IsOpenProperty); }
			set { SetValue(IsOpenProperty, value); }
		}

		// Using a DependencyProperty as the backing store for IsOpen.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty IsOpenProperty =
			DependencyProperty.Register("IsOpen", typeof(bool), typeof(DialogPresenter), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsOpenChanged));


		static void OnIsOpenChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
		{
			DialogPresenter presenter = (DialogPresenter)target;

			bool isOpen = (bool)e.NewValue;
			if (isOpen)
			{
				presenter.Dispatcher.BeginInvoke((Action)presenter.ShowDialog);
			}
			else
			{
				presenter.Dispatcher.BeginInvoke((Action)presenter.CloseDialog);
			}
		}

		private void ShowDialog()
		{
			// Create host window
			Window hostWindow = this.HostWindow;
			if (hostWindow == null)
			{
				ContentPresenter contentPresenter = new ContentPresenter()
				{
					Content = this.DialogContent,
					ContentTemplate = this.DialogContentTemplate
				};

				hostWindow = new Window();
				hostWindow.SizeToContent = SizeToContent.WidthAndHeight;
				hostWindow.DataContext = this.DataContext;
				hostWindow.SetBinding(Window.StyleProperty, new Binding() { Source = this, Path = new PropertyPath(WindowStyleProperty) });
				hostWindow.Content = contentPresenter;

				this.HostWindow = hostWindow;
			}

			hostWindow.ShowDialog();

			this.IsOpen = false;
		}
		private void CloseDialog()
		{
			Window hostWindow = this.HostWindow;
			if (hostWindow != null)
			{
				hostWindow.Close();
				this.HostWindow = null;
			}
		}

		public Style WindowStyle
		{
			get { return (Style)GetValue(WindowStyleProperty); }
			set { SetValue(WindowStyleProperty, value); }
		}

		// Using a DependencyProperty as the backing store for WindowStyle.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty WindowStyleProperty =
			DependencyProperty.Register("WindowStyle", typeof(Style), typeof(DialogPresenter), new UIPropertyMetadata(null));


	}
}
